package widget

import (
	"clientDemo/service/widget_data"
	"clientDemo/ui/fyne_ui/common"
	"clientDemo/ui/fyne_ui/widget/interface"
	"os"
)

type SkinXmlParser struct {
	path string

	skinList    []widgetcommon.IWidgetSkin
	widgetInfos map[string]widgetcommon.IWidgetSkin
}

func NewSkinXmlParser(path string) *SkinXmlParser {
	o := &SkinXmlParser{
		path:        path,
		widgetInfos: make(map[string]widgetcommon.IWidgetSkin),
	}

	data, err := os.ReadFile(path)
	if nil != err || len(data) == 0 {
		return nil
	}
	loader := widget_data.NewSkinLoader(o)
	loader.Load(data)
	return o
}
func (p *SkinXmlParser) UpdateProperties(prop []*widget_data.Property) {

}

func (p *SkinXmlParser) UpdateLoadSkin(skinInfo *widget_data.LoadSkin) {
}
func (p *SkinXmlParser) StartElement(ctx interface{}, baseWidgetName string, atts interface{}) {
	if !CheckWidgetName(baseWidgetName) {
		return
	}
	data, ok := atts.(*widget_data.SkinData)
	if !ok {
		return
	}
	info := common.NewSkinInfo(baseWidgetName, data.SkinName)

	for k, v := range data.AttributeMap {
		info.SetAttribute(k, v)
	}
	for i := 0; i < len(data.Properties); i++ {
		property := data.Properties[i]
		if property == nil {
			continue
		}
		info.AddProperty(property.Key, property.Value)
		//info.SetProperty(property.Key, property.Value)
	}
	p.skinList = append(p.skinList, info)
	p.widgetInfos[data.SkinName] = info
	common.Get().AddSkin(data.SkinName, info)
}
func (p *SkinXmlParser) EndElement(ctx interface{}, skinName string, atts interface{}) {

}
